package com.example.wm.SanWar.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.SanWar.entity.AlliesandUser;
import com.example.wm.SanWar.entity.Pointsrecord;
import com.example.wm.SanWar.entity.RosterMemberList;
import com.example.wm.SanWar.mapper.AlliesandUserMapper;
import com.example.wm.SanWar.mapper.PointsrecordMapper;
import com.example.wm.SanWar.service.AlliesandUserService;
import com.example.wm.SanWar.service.PointsrecordService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/PointsrecordController")
public class PointsrecordController {
    @Resource
    private PointsrecordService pointsrecordService;
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private PointsrecordMapper pointsrecordMapper;
    @Resource
    private AlliesandUserMapper alliesandUserMapper;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody Pointsrecord pointsrecord) {

            LambdaQueryWrapper<AlliesandUser> lq=new LambdaQueryWrapper<>();
            lq.eq(AlliesandUser::getName,pointsrecord.getRewordpoeple());
            lq.eq(AlliesandUser::getStatus,"在盟");
            AlliesandUser alliesandUser=    alliesandUserMapper.selectOne(lq);
            pointsrecord.setCompetitionseason(alliesandUser.getCompetitionseason());

            Integer finnalpoints=0;
            int finnalpoints2=0;

            if(alliesandUser!=null){
                if(Objects.equals(pointsrecord.getType(), "奖励积分")){
                    if(alliesandUser.getPointesAll()!=null){
                    if(Objects.equals(pointsrecord.getStatuss(),"奖励")){
                        finnalpoints= Integer.parseInt(alliesandUser.getPointesAll())+pointsrecord.getPoints();

                    }else {
                        finnalpoints=Integer.parseInt(alliesandUser.getPointesAll())- pointsrecord.getPoints();

                    }
                    }else {
                        if(Objects.equals(pointsrecord.getStatuss(),"奖励")){
                            finnalpoints=pointsrecord.getPoints();

                        }else {
                            finnalpoints=-pointsrecord.getPoints();

                        }
                    }

                    alliesandUser.setPointesAll(String.valueOf(finnalpoints));
                    alliesandUser.setTurnovervalueaLL(String.valueOf(finnalpoints));
                    alliesandUserMapper.updateById(alliesandUser);
                    pointsrecord.setFinnalpoints(finnalpoints);
                } else if (Objects.equals(pointsrecord.getType(), "奖励可用积分")) {
                    if(alliesandUser.getPointes()!=null){
                        if(Objects.equals(pointsrecord.getStatuss(),"奖励")){

                            finnalpoints=Integer.parseInt(alliesandUser.getPointes())+pointsrecord.getPoints();
                            finnalpoints2=Integer.parseInt(alliesandUser.getMilitaryIndustryall())+pointsrecord.getPoints();
                        }else {
                            finnalpoints=Integer.parseInt(alliesandUser.getPointes())-pointsrecord.getPoints();
                            finnalpoints2=Integer.parseInt(alliesandUser.getMilitaryIndustryall())-pointsrecord.getPoints();

                        }
                    }else {
                        if(Objects.equals(pointsrecord.getStatuss(),"奖励")){

                            finnalpoints=pointsrecord.getPoints();
                            finnalpoints2=pointsrecord.getPoints();
                        }else {
                            finnalpoints= -pointsrecord.getPoints();
                            finnalpoints2=-pointsrecord.getPoints();
                        }
                    }

                    alliesandUser.setMilitaryIndustryall(String.valueOf(finnalpoints2));
                    alliesandUser.setTurnovermilitaryvalue(String.valueOf(finnalpoints2));
                    alliesandUser.setPointes(String.valueOf(finnalpoints));
                    alliesandUser.setTurnovervalue(String.valueOf(finnalpoints));

                    alliesandUserMapper.updateById(alliesandUser);
                    pointsrecord.setFinnalpoints(finnalpoints);
                } else if (Objects.equals(pointsrecord.getType(), "奖励脸蛋")) {
                    if(alliesandUser.getFaces()!=null){
                    if(Objects.equals(pointsrecord.getStatuss(),"奖励")){
                       finnalpoints=Integer.parseInt(alliesandUser.getFaces())+pointsrecord.getPoints();
                    }else {
                        finnalpoints=Integer.parseInt(alliesandUser.getFaces())-pointsrecord.getPoints();
                    }
                    }else {
                        if(Objects.equals(pointsrecord.getStatuss(),"奖励")){
                            finnalpoints=pointsrecord.getPoints();
                        }else {
                            finnalpoints=-pointsrecord.getPoints();
                        }
                    }
                    alliesandUser.setFaces(String.valueOf(finnalpoints));
                    alliesandUserMapper.updateById(alliesandUser);
                    pointsrecord.setFinnalpoints(finnalpoints);
                }else if (Objects.equals(pointsrecord.getType(), "积分申请")) {
                    if(Objects.equals(pointsrecord.getType2(), "可用积分")){

                    if(alliesandUser.getPointes()!=null ){
                        if(Objects.equals(pointsrecord.getStatuss(),"奖励")){
                            finnalpoints=Integer.parseInt(alliesandUser.getPointes())+pointsrecord.getPoints();
                            finnalpoints2=Integer.parseInt(alliesandUser.getMilitaryIndustryall())+pointsrecord.getPoints();

                        }else {
                            finnalpoints=Integer.parseInt(alliesandUser.getPointes())-pointsrecord.getPoints();
                            finnalpoints2=Integer.parseInt(alliesandUser.getMilitaryIndustryall())-pointsrecord.getPoints();

                        }
                    }else {
                        if(Objects.equals(pointsrecord.getStatuss(),"奖励")){

                            finnalpoints=pointsrecord.getPoints();
                            finnalpoints2=pointsrecord.getPoints();

                        }else {
                            finnalpoints= -pointsrecord.getPoints();
                            finnalpoints2=-pointsrecord.getPoints();

                        }
                    }
                        alliesandUser.setMilitaryIndustryall(String.valueOf(finnalpoints2));
                      alliesandUser.setTurnovermilitaryvalue(String.valueOf(finnalpoints2));
                        alliesandUser.setPointes(String.valueOf(finnalpoints));
                        alliesandUser.setTurnovervalue(String.valueOf(finnalpoints));
                        alliesandUserMapper.updateById(alliesandUser);
                        pointsrecord.setFinnalpoints(finnalpoints);


                    }else{
                        if(alliesandUser.getPointesAll()!=null){
                            if(Objects.equals(pointsrecord.getStatuss(),"奖励")){
                                finnalpoints=Integer.parseInt(alliesandUser.getPointesAll())+pointsrecord.getPoints();

                            }else {
                                finnalpoints=Integer.parseInt(alliesandUser.getPointesAll())-pointsrecord.getPoints();

                            }
                        }else {
                            if(Objects.equals(pointsrecord.getStatuss(),"奖励")){
                                finnalpoints=pointsrecord.getPoints();

                            }else {
                                finnalpoints=-pointsrecord.getPoints();

                            }
                        }
                        alliesandUser.setPointesAll(String.valueOf(finnalpoints));
                        alliesandUser.setTurnovervalueaLL(String.valueOf(finnalpoints));
                        alliesandUserMapper.updateById(alliesandUser);
                        pointsrecord.setFinnalpoints(finnalpoints);

                    }


                }
                pointsrecord.setCreatetime(tsyUtils.getnowtime());
                Boolean b= pointsrecordService.save(pointsrecord);
                return ResultData.success(pointsrecord.getId());

            }else{
                return ResultData.error(202,"用户信息不存在");

            }




    }

    @Transactional
    @PostMapping("/find")
    public Result find(@RequestBody JSONObject jsonObject) {
        LambdaQueryWrapper<Pointsrecord> lq=new LambdaQueryWrapper<>();
        lq.eq(Pointsrecord::getType2,jsonObject.getString("type2"));
        lq.eq(Pointsrecord::getRewordpoepleid,jsonObject.getString("rewordpoepleid"));
        lq.ne(Pointsrecord::getType,"奖励脸蛋");
        lq.eq(Pointsrecord::getCompetitionseason,jsonObject.getString("competitionseason"));
        lq.orderByDesc(Pointsrecord::getCreatetime);
        Page<Pointsrecord> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));

        Page<Pointsrecord> list=pointsrecordService.page(pageInfo,lq);
        if(list.getSize()>0){
            for (Pointsrecord pointsrecord : list.getRecords()){
                LambdaQueryWrapper<AlliesandUser> lqa=new LambdaQueryWrapper<>();
                lqa.eq(AlliesandUser::getId,pointsrecord.getRewordpoepleid());
                lqa.eq(AlliesandUser::getStatus,"在盟");
                AlliesandUser one = alliesandUserMapper.selectOne(lqa);
                if(!Objects.equals(one.getName(), pointsrecord.getRewordpoeple())) {
                    pointsrecord.setRewordpoeple(one.getName());
                }
                LambdaQueryWrapper<AlliesandUser> lqa1=new LambdaQueryWrapper<>();
                lqa1.eq(AlliesandUser::getId,pointsrecord.getCreatenameid());
                lqa1.eq(AlliesandUser::getStatus,"在盟");
                AlliesandUser one1 = alliesandUserMapper.selectOne(lqa1);
                if(!Objects.equals(one1.getName(), pointsrecord.getCreatename())) {
                    pointsrecord.setCreatename(one1.getName());
                    pointsrecord.setCreatappoint(one1.getAppoint());
//                    System.out.println("names1"+one1.getName()+one1.getAppoint());
                }
            }

        }

        return ResultData.success(list);
    }

}
